package com.qiedan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiedan.bean.Item;
import com.qiedan.bean.Orders;

import com.qiedan.dto.OrdersDto;
import com.qiedan.mapper.ItemMapper;
import com.qiedan.service.OrdersService;
import com.qiedan.mapper.OrdersMapper;
import com.qiedan.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{

    @Autowired
    ItemMapper itemMapper;
    /**
     * 根据用户名获取订单
     * @param uid
     * @return
     */
    @Override
    public R<Object> getOrdersListByUserId(Integer uid,Integer pageIndex,Integer pageSize) {


        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        //分页查询数据
        Page<Orders> page = new Page<>(pageIndex,pageSize);
        //分页查询结果
        Page<Orders> ordersList = baseMapper.selectPage(page, queryWrapper);

        List<OrdersDto> list = new ArrayList<>();
        for(Orders orders : ordersList.getRecords()){
            //商品id
            Integer iid = orders.getIid();
            Item item = itemMapper.selectById(iid);
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            String name = item.getName(); //商品名字
            ordersDto.setItemName(name);
            Integer status = orders.getStatus(); //0待付款、1待发货、2已发货、3已收货、4已退款
            if(status==0)
                ordersDto.setStatus("待付款");
            else if(status==1)ordersDto.setStatus("待发货");
            else if(status==2)ordersDto.setStatus("已发货");
            else if(status==3)ordersDto.setStatus("已收货");
            else if(status==4)ordersDto.setStatus("已退款");
            list.add(ordersDto);
        }
        Page<OrdersDto> pageInfo = new Page<>(pageIndex,pageSize);
        pageInfo.setRecords(list);
        long total = ordersList.getTotal();
        pageInfo.setTotal(total);

        log.info("pageInfo: "+pageInfo);
        R<Object> resp = new R<Object>();
        resp.add("pageInfo",pageInfo);

        return resp;
    }
}




